
= form_tag questionable.answers_path, method: :post, class: 'questions-form' do
  - assignments.each do |a|
    .question-block{:id => "qa_#{a.id}", class: "question-#{a.question.id}"}
      - q = a.question

      .question-and-note
        .question= q.title
        .note= q.note if q.note

      - selected_answers = a.answers_for_user(current_user)
      - selected_option_ids = selected_answers.map(&:option_id)
      - field = "answers[#{a.id}][]"

      - if q.accepts_multiple_answers?
        / Some inputs can take multiple answers (namely multiselects and checkboxes)
        - if q.input_type == 'checkboxes'
          = hidden_field_tag field
          - q.options.each do |opt|
            %label.checkbox
              = check_box_tag field, opt.id, selected_option_ids.include?(opt.id)
              = opt.title
        - elsif q.input_type == 'multiselect'
          = select_tag field,
            options_from_collection_for_select(q.options, 'id', 'title', selected_option_ids), multiple: true

      - else
        / Most inputs just take a single answer
        - if q.input_type == 'radio'
          - q.options.each do |opt|
            %label.radio
              %input{type: :radio, name: field, value: opt.id, checked: selected_option_ids.first == opt.id}
              = opt.title
        - else
          .compact-input
            - if q.input_type == 'select'
              = select_tag "answers[#{a.id}][]",
                options_from_collection_for_select(q.options, 'id', 'title', selected_option_ids.first), prompt: 'Choose an option'
            - elsif q.input_type == 'string'
              = text_field_tag field, selected_answers.first.try(:message)
            - elsif q.input_type == 'date'
              =# select_date nil, field, order: [:month, :day, :year]
              =# date_select (Date.today - 20.years), field, order: [:month, :day, :year]

              - date = selected_answers.first.try(:date_answer)

              .date-fields
                = select_month date, {prompt: date.blank?}, name: "#{field}[month]", class: 'select-month'
                = select_day   date, {prompt: date.blank?}, name: "#{field}[day]", class: 'select-day'
                = select_year  date,
                  {prompt: date.blank?, start_year: (Date.today-13.years).year, end_year: (Date.today-70.years).year},
                  name: "#{field}[year]", class: 'select-year'

  .form-actions
    - label = local_assigns.has_key?(:submit_label) ? submit_label : 'Save'
    = submit_tag label, class: 'btn'

